// pages/myself/myself.js
import api from '../../utils/api.js';
import network from '../../utils/network.js';
import notice from '../../utils/notice.js';
import utils from '../../utils/utils';
import formatDate from '../../utils/format_date';

const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        visibleOpen: false,
        list: [],
        tabs:[
            {
                title: '我发布的'
            },
            {
                title: '我报名的'
            }
        ],
        tabList: ['全部', '未开始', '进行中', '已结束', '已下架'],
        activityList: [
          {
            cover: 'http://devhui.salonhui.com/assets/cover1.png',
            title: '测试标题1111',
            status: '10'
          },
          {
            cover: 'http://devhui.salonhui.com/assets/cover2.png',
            title: '测试标题3131313131313131313',
            status: '30'
          },
          {
            cover: 'http://devhui.salonhui.com/assets/cover1.png',
            title: '测试标题14141414141414141414dada试标题试标题',
            status: '60'
          },
          {
            cover: 'http://devhui.salonhui.com/assets/cover2.png',
            title: '测试标题1111试标题试标题试标题试标题试标题试标题试标题试标题试标题试标题',
            status: '90'
          },
        ],
        statusList: {
          10: "未开始",
          30: "进行中",
          60: "已结束",
          90: "已下架"
        },
        currentTab: 0,
        avatar: '/assets/avator.png'
        // avatar: '/assets' + app.globalData.style.month + '/avatar.png'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.windowInfo = wx.getWindowInfo();
        this.setData({
            statusBarHeight: this.windowInfo.statusBarHeight,
        })

        notice.addNotice("UserInfo", this.refreshUserInfo, this);
    },
    onShow() {
        let userInfo = app.globalData.userInfo || 0;
        let expiration = new Date(userInfo.expirationDate).getTime();
        if (expiration < new Date().getTime()) {
            userInfo.expirationDate = 0
        } else {
            userInfo.expirationDate = formatDate.formatTime(new Date(userInfo.expirationDate), "yyyy-MM-dd");
        }
        this.setData({
            userInfo
        })
        if (typeof this.getTabBar === 'function' &&
            this.getTabBar()) {
            this.getTabBar().setData({
                selected: 2
            })
        }
    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        notice.removeNotice("UserInfo", this);
    },
    tabsChange (e) {
        console.log(e);
    },
    switchTab (e) {
      let current = e.currentTarget.dataset.current
      console.log(current);
      this.setData({ currentTab: current  })
    },
    chooseAvatar(e) {
        network.uploadFile(e.detail.avatarUrl, 'file', {}).then(img => {
            app.globalData.userInfo.avatar = img;
            return network.postJson(api.updateUser, app.globalData.userInfo);
        }).then(res => {
            this.refreshUserInfo();
        })
    },
    goEdit () {
      wx.navigateTo({
        url: '/active/active-details/active-details',
      })
    },
    goInfo() {
        wx.navigateTo({
            url: '/myself/user/info',
        })
    },
    goMyOrder () {
        wx.navigateTo({
            url: '/myself/order/list',
        })
    },
    login() {
        wx.navigateTo({
            url: '/myself/login/login'
        })
    },

    goOrderList(e) {
        if (app.globalData.userInfo.mobile) {
            wx.navigateTo({
                url: '/myself/order/list?idx=' + e.currentTarget.dataset.idx
            })
        } else {
            this.login();
        }
    },

    goReceived() {
        if (app.globalData.userInfo.mobile) {
            wx.navigateTo({
                url: '/myself/received/received'
            })
        } else {
            this.login();
        }
    },

    goMyPromotion() {
        if (app.globalData.userInfo.mobile) {
            wx.navigateTo({
                url: '/myself/my-promotion/my-promotion',
            })
        } else {
            this.login();
        }
    },

    goSendActive() {
        if (app.globalData.userInfo.mobile) {
            wx.navigateTo({
                url: '/active/send-active/send-active',
            })
        } else {
            this.login();
        }
    },

    goMyActive(e) {
        if (app.globalData.userInfo.mobile) {
            wx.navigateTo({
                url: '/myself/enroll/my-active'
            })
        } else {
            this.login();
        }
    },

    refreshUserInfo() {
        let userInfo = app.globalData.userInfo;
        this.setData({
            userInfo
        })
    },

    goAgreement() {
        wx.navigateTo({
            url: '/myself/agreement/agreement',
        })
    },

    goPrivacy() {
        wx.navigateTo({
            url: '/myself/privacy/privacy',
        })
    },

    goKefu() {
        wx.makePhoneCall({
            phoneNumber: '18971177132'
        })
    },

    goAbout() {
        wx.navigateTo({
            url: '/myself/about/about',
        })
    },

    openTap() {
        this.setData({
            visibleOpen: true
        })
    },

    onVisibleOpenChange(e) {
        this.setData({
            visibleOpen: e.detail.visible
        })
    },

    openVIPSuccess(e) {
        let vip = e.detail;
        let expiration = new Date(app.globalData.userInfo.expirationDate || 0).getTime();
        if (expiration < new Date().getTime()) {
            expiration = new Date().getTime();
        }
        if (vip.label == '月卡会员') {
            expiration += 30 * 24 * 60 * 60 * 1000;
        } else if (vip.label == '月卡会员') {
            expiration += 365 * 24 * 60 * 60 * 1000;
        }
        app.globalData.userInfo.expirationDate = formatDate.formatTime(expiration, "yyyy-MM-dd HH:mm:ss")
        app.globalData.userInfo.vipType = vip.label;
        this.setData({
            [`userInfo.vipType`]: vip.label,
            [`userInfo.expirationDate`]: formatDate.formatTime(expiration, "yyyy-MM-dd")
        })
        wx.reLaunch({
            url: '/home/success/success?mode=VIP'
        })
    }

})